package com.Service;

import com.Dao.AddressDao;
import com.Dao.UserDao;
import com.POJO.User;
import com.Service.Imp.UserServiceImp;
import com.Utils.UserName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@Service
public class UserService extends ServiceImpl<UserDao,User> implements UserServiceImp{

    @Autowired
    private UserDao userDao;

    /**查找电话号是否已经注册
     *
     * @param All_Map
     * @return
     */
    public boolean UserFindTel(Map<String, String> All_Map){
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("tel",All_Map.get("tel"));
        return userDao.exists(userQueryWrapper);
    }

    /**普通用户注册
     *
     * @param All_Map
     * @return
     */
    public int UserRegister(Map<String, String> All_Map){
        User user=new User();
        user.setUsername("用户"+UserName.generateRandomString(10));
        user.setHeadshot("default.jpg");
        user.setTel(All_Map.get("tel"));
        user.setPwd(All_Map.get("pwd"));
        return userDao.insert(user);
    }

    /**查询用户个人数据
     *
     *
     * @param tel
     * @return
     */
    public User FindUser(String tel){
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("tel",tel);
        return userDao.selectOne(userQueryWrapper);
    }
    /**
     * 用户登录判断
     *
     * @param tel
     * @return
     */
    public User Login(String tel,String pwd){
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("tel",tel);
        userQueryWrapper.eq("pwd",pwd);
        return userDao.selectOne(userQueryWrapper);
    }

    /**设置用户默认地址
     *
     * @param Map_All
     * @return
     */
    public int UseraddAddress(Map<String,String> Map_All){
        String tel =Map_All.get("tel");
        String province =Map_All.get("province");
        String city =Map_All.get("city");
        String area =Map_All.get("area");
        String addressIp =Map_All.get("addressIp");
        String longitude =Map_All.get("longitude");
        String latitude =Map_All.get("latitude");
        User user=new User();
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.set("province",province);
        wrapper.set("city",city);
        wrapper.set("area",area);
        wrapper.set("address_Ip",addressIp);
        wrapper.set("longitude",longitude);
        wrapper.set("latitude",latitude);
        wrapper.eq("tel",tel);
        return userDao.update(user,wrapper);
    }

    /**
     *  上传头像
     * @param tel
     * @param headshot
     * @return
     */
    public int updataHeadshot(String tel,MultipartFile headshot){
            //原始文件名
            String originalFilename = headshot.getOriginalFilename();//xxx.png
            assert originalFilename != null;
            //对原始名进行截取"."后面的字符
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));//.png
            //使用UUID重新生成文件名，防止文件名称重复造成文件覆盖
            String fileName = tel + suffix;//手机号命名
            File basePath = new File("src/main/resources/static/Headshot");
            // 不存在，则创建该文件夹
//            if(!basePath.exists()){basePath.mkdir();}
            String Path = basePath.getAbsolutePath() + File.separator + fileName;
            try {
                File file=new File(Path);
                //将临时文件转存到指定位置
                headshot.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        User user=new User();
        UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
        userUpdateWrapper.set("headshot",fileName);
        userUpdateWrapper.eq("tel",tel);
        return userDao.update(user,userUpdateWrapper);
    }
    public int updataPwd(String tel,String pwd){
        User user=new User();
        UpdateWrapper<User>  userUpdateWrapper=new UpdateWrapper<>();
        userUpdateWrapper.set("pwd",pwd);
        userUpdateWrapper.eq("tel",tel);
        return userDao.update(user,userUpdateWrapper);
    }
    public int updataUsername(String tel,String username){
        User user=new User();
        UpdateWrapper<User>  userUpdateWrapper=new UpdateWrapper<>();
        userUpdateWrapper.set("username",username);
        userUpdateWrapper.eq("tel",tel);
        return userDao.update(user,userUpdateWrapper);
    }

}
